package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.pojo.Product;
import com.ruoyi.service.ColllectService;
import com.ruoyi.service.ProductService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.R;
import com.ruoyi.vo.ProductDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ColllectService colllectService;


    @GetMapping("/getinfo")
    public R<SysUser> getInfo(@RequestParam Integer id){
        SysUser sysUser = iSysUserService.selectUserById(id.longValue());
        return R.success(sysUser);
    }

    @GetMapping("/productList")
    public R<List<ProductDto>> productList(Product product){
        System.out.println(product);
        QueryWrapper wrapper = new QueryWrapper<>();
        if (product.getTitle()!=null&&!product.getTitle().equals("")){
            wrapper.like("title",product.getTitle());
        }
        if(product.getId()!=null&&!product.getId().equals("")){
            wrapper.eq("id",product.getId());
        }
        if(product.getUserid()!=null&&!product.getUserid().equals("")){
            wrapper.eq("userid",product.getUserid());
        }
        List<Product> list = productService.list(wrapper);
        List<ProductDto> dtoList = new ArrayList<>();
        for (Product i : list){
            ProductDto productDto = new ProductDto();
            SysUser sysUser = iSysUserService.selectUserById(i.getUserid().longValue());
            if (i.getImage() != null && i.getImage().length()>0) {
                String[] split = i.getImage().split(",");
                productDto.setImg(Arrays.asList(split));
            }else{
                productDto.setImg(new ArrayList());
            }
            productDto.setUser(sysUser);
            productDto.setUserid(i.getUserid());
            productDto.setContent(i.getContent());
            productDto.setColor(i.getColor());
            productDto.setBuyWay(i.getBuyWay());
            productDto.setCreateDate(i.getCreateDate());
            productDto.setDamageDegree(i.getDamageDegree());
            productDto.setId(i.getId());
            productDto.setImage(i.getImage());
            productDto.setPrice(i.getPrice());
            productDto.setTitle(i.getTitle());
            productDto.setPhone(i.getPhone());
            productDto.setOther(i.getOther());
            dtoList.add(productDto);
        }
        return R.success(dtoList);
    }

    @DeleteMapping("/removeproduct")
    public R<String> removeProduct(@RequestBody Product product){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("productid",product.getId());
        boolean remove = colllectService.remove(wrapper);
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("id",product.getId());
        boolean b = productService.remove(wrapper1);
        if (b) {
            return R.success("删除成功");
        }else
            return R.error("删除失败");
    }

    @PostMapping("/addproduct")
    public R<String> addProduct(@RequestBody Product product){
        product.setCreateDate(new Date());
        int i = productService.addProdut(product);
        if (i > 0) {
            return R.success("发布成功");
        }else{
            return R.error("发布失败");
        }
    }

}
